#include "gameanimation.h"


GameSeqAnimation::GameSeqAnimation(QObject* parent)
    :QSequentialAnimationGroup(parent)
{
}



void GameSeqAnimation::addMove(QObject* _target, QPointF moveS, QPointF moveE, int duration){
    QPropertyAnimation* moveAnim = new QPropertyAnimation(_target, "pos");
    moveAnim->setDuration(duration);
    moveAnim->setStartValue(moveS);
    moveAnim->setEndValue(moveE);
    this->addAnimation(moveAnim);
}

void GameSeqAnimation::addFade(QObject *_target, qreal fadeS, qreal fadeE, int duration, QEasingCurve curve){
    QPropertyAnimation* fadeAnim = new QPropertyAnimation(_target, "opacity");
    fadeAnim->setDuration(duration);
    fadeAnim->setStartValue(fadeS);
    fadeAnim->setEndValue(fadeE);
    fadeAnim->setEasingCurve(curve);
    this->addAnimation(fadeAnim);
}

void GameSeqAnimation::addRotation(QObject *_target, qreal rotS, qreal rotE, int duration, bool loop){
    QPropertyAnimation* rotAnim = new QPropertyAnimation(_target, "rotation");
    if(loop)
        rotAnim->setLoopCount(-1);
    rotAnim->setDuration(duration);
    rotAnim->setStartValue(rotS);
    rotAnim->setEndValue(rotE);
    this->addAnimation(rotAnim);
}


void GameSeqAnimation::startUntilStop(QAbstractAnimation::DeletionPolicy p){
    connect(this, SIGNAL(finished()), this, SLOT(release()));
    start(p);
    e.exec();
}

void GameSeqAnimation::release(){
    //disconnect signal slot
    disconnect(this, SIGNAL(finished()), this, SLOT(release()));
    //release lock
    e.exit();
}

GameParAnimation::GameParAnimation(QObject *parent)
    :QParallelAnimationGroup(parent)
{

}

void GameParAnimation::addMove(QObject* _target, QPointF moveS, QPointF moveE, int duration){
    QPropertyAnimation* moveAnim = new QPropertyAnimation(_target, "pos");
    moveAnim->setDuration(duration);
    moveAnim->setStartValue(moveS);
    moveAnim->setEndValue(moveE);
    this->addAnimation(moveAnim);
}

void GameParAnimation::addFade(QObject *_target, qreal fadeS, qreal fadeE, int duration){
    QPropertyAnimation* fadeAnim = new QPropertyAnimation(_target, "opacity");
    fadeAnim->setDuration(duration);
    fadeAnim->setStartValue(fadeS);
    fadeAnim->setEndValue(fadeE);
    this->addAnimation(fadeAnim);
}

void GameParAnimation::addScale(QObject *_target, qreal scaleS, qreal scaleE, int duration){
    QPropertyAnimation* scaleAnim = new QPropertyAnimation(_target, "scale");
    scaleAnim->setDuration(duration);
    scaleAnim->setStartValue(scaleS);
    scaleAnim->setEndValue(scaleE);
    this->addAnimation(scaleAnim);
}

void GameParAnimation::addRotation(QObject *_target, qreal rotS, qreal rotE, int duration, bool loop){
    QPropertyAnimation* rotAnim = new QPropertyAnimation(_target, "rotation");
    if(loop)
        rotAnim->setLoopCount(-1);
    rotAnim->setDuration(duration);
    rotAnim->setStartValue(rotS);
    rotAnim->setEndValue(rotE);
    this->addAnimation(rotAnim);
}


void GameParAnimation::startUntilStop(QAbstractAnimation::DeletionPolicy p){
    connect(this, SIGNAL(finished()), this, SLOT(release()));
    start(p);
    e.exec();
}



void GameParAnimation::release(){
    //disconnect signal slot
    disconnect(this, SIGNAL(finished()), this, SLOT(release()));
    //release lock
    e.exit();
}
